home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4721 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.8 KB  |  113 lines

  1. Path: news2.ios.com!usenet
  2. From: vlad@gramercy.ios.com (Vlastimil Adamovsky)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Strange template behavior with Borland C++
  5. Date: Wed, 31 Jan 1996 21:14:48 GMT
  6. Organization: Internet Online Services
  7. Message-ID: <4eolj5$1fl@news2.ios.com>
  8. References: <4ekhnp$6pe@hemuli.tte.vtt.fi>
  9. NNTP-Posting-Host: ppp-33.ts-7.hck.idt.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Ali Lattunen <Ali.Lattunen@vtt.fi> wrote:
  13.  
  14.  
  15. >Hello all!
  16.  
  17. >Has anyone else had problems with using templates with Borland C++ 4.5x
  18. >compiler? I still haven't got the idea of how templates are treated by
  19. >the compiler.
  20.  
  21. >I have done everything according to the rules mentioned in the manuals:
  22. >included both my template class declarations and member function
  23. >implementations in the header files and compiled the whole project using
  24. >the -Jg switch (smart templates).
  25.  
  26. >However, the compiler is able to create only one instance of each member
  27. >function. The link-time error occurs if member functions of several types
  28. >of template classes are called.
  29.  
  30. >If I have, for example, template class TemplClass<class T> with member
  31. >function TemplClass<T>::Foo(), and have template instancies
  32. >TemplClass<MyClass1> C1 and TemplClass<MyClass2> C2, calling foo as
  33. >C1.Foo() works fine. But the C2.Foo() causes a linker error:
  34.  
  35. >Undefined symbol TempClass<MyClass2>::Foo() in module...
  36.  
  37. >What the heck? The problem is avoided if Foo is defined inline!?
  38. >But why? What is the point in this?
  39.  
  40. I have not seen your code. But I have some code that maybe can help
  41. you. I am using Symantec ver. 7.2 . I don't even know what switches I
  42. used. I don't care as far it works:
  43.  
  44. ************************************pokus.h****************************************
  45. template <class T> class A
  46. {
  47.   T x;
  48.   public:
  49.   A(T i) : x(i) {}
  50.   operator float() ;
  51.   operator int() ;
  52.   operator short();
  53.   operator double();
  54.   A(A<int> & i): x((T)i) {}
  55. };
  56.  
  57. template <class T> A<T>::operator float()
  58.    {
  59.     return (float) x;
  60.    }
  61.  
  62. template <class T> A<T>::operator int()
  63.    {
  64.     return (int) x;
  65.    }
  66.  
  67. template <class T> A<T>::operator short()
  68.    {
  69.     return (short) x;
  70.    }
  71.  
  72. template <class T> A<T>::operator double()
  73.    {
  74.     return (double) x;
  75.    }
  76.  
  77.  
  78. void doItInAnotherSourceFile(A<float> & a);
  79. ******************************end of *********************************
  80. *
  81. **************pokus.cpp**************************
  82.  
  83. #include "pokus.h"
  84.  
  85. void main()
  86. {
  87.   A<int> a(6);
  88.   A<float> x = a;
  89.   doItInAnotherSourceFile(x);
  90.   A<short> i = x;
  91.   A<double> z = i;
  92. };
  93. ****************end of pokus.cpp*******
  94.  
  95. ******pokus2.cpp******
  96. #include "pokus.h"
  97.  
  98. void doItInAnotherSourceFile(A<float> & a)
  99.  {
  100.    A<short> i = a;
  101.    A<double> z = i;
  102.  }
  103. *************************
  104.  
  105. NO PROBLEM!!!
  106.  
  107.  
  108. *******************************************
  109. *    Vlastimil Adamovsky                  *
  110. * Smalltalk, C++ and Envelop development  *
  111. *******************************************
  112.  
  113.